package com.si.anddos;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

	public class ShowOptionsActivity extends OptionsMenuActivity {
		
		private Button buttonSave=null;
		private EditText NSearchs=null;
		private CheckBox checkboxRapidshare=null;
		private CheckBox checkboxMegaupload=null;
		private CheckBox checkboxFileserve=null;
		private CheckBox checkboxMegashares=null;
		private CheckBox checkboxFilesonic=null;
		private CheckBox checkboxHotfile=null;
		private Spinner spinnerExtension=null;
		private String StringSpinner=null;
		private int SpinnerPos=-1;
		
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.options_search); 
			setTitle(R.string.titleOptions);
			
			
			
			NSearchs=(EditText)findViewById(R.id.editTextNumberSearchs);
			
			checkboxRapidshare=(CheckBox)findViewById(R.id.checkboxRapidshare);
			
			checkboxMegaupload=(CheckBox)findViewById(R.id.checkboxMegaupload);
			
			checkboxFileserve=(CheckBox)findViewById(R.id.checkboxFileserve);
			
			checkboxMegashares=(CheckBox)findViewById(R.id.checkboxMegashares);
			
			checkboxHotfile=(CheckBox)findViewById(R.id.checkboxHotfile);
			
			checkboxFilesonic=(CheckBox)findViewById(R.id.checkboxFilesonic);
			
			spinnerExtension = (Spinner) findViewById(R.id.spinnerExtension);
			ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
			            this, R.array.extensions_array, android.R.layout.simple_spinner_item);
			adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			spinnerExtension.setAdapter(adapter);
			spinnerExtension.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){

				public void onItemSelected(AdapterView<?> parent, View view,
						int pos, long id) {
					// TODO Auto-generated method stub
					
					 StringSpinner=parent.getItemAtPosition(pos).toString();
					 SpinnerPos=pos;
					 //Toast.makeText(parent.getContext(),"La posicion es " + StringSpinner, Toast.LENGTH_LONG).show();
				}

				public void onNothingSelected(AdapterView<?> arg0) {
					// TODO Auto-generated method stub
					
				}
					
			});
			
			fillOptions();
					
			buttonSave=(Button)findViewById(R.id.buttonSaveOptions);
			buttonSave.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					saveOptions();
				}
			});
			
		}
		
		private void fillOptions(){
			try{
				checkboxRapidshare.setChecked(Globals.rapidshare);
				checkboxMegaupload.setChecked(Globals.megaupload);
				checkboxFileserve.setChecked(Globals.fileserve);
				checkboxMegashares.setChecked(Globals.megashares);
				checkboxHotfile.setChecked(Globals.hotfile);
				checkboxFilesonic.setChecked(Globals.filesonic);
				NSearchs.setText(Integer.toString(Globals.MAX_SEARCHS));
				spinnerExtension.setSelection(Globals.POS_EXTENSION);
			}
			catch(Exception e){
				Log.w(SEARCH_SERVICE, "Error filling the options");
			}
			
		}
		
		
		private void saveOptions(){
			//save the state of the options
			String aux=null;
			int flag=0;
			
			try{
				aux=NSearchs.getText().toString();
				if(aux.length()==1){
					if(aux.charAt(0)<'1' || aux.charAt(0)>'9'){
						flag=1;
					}
				}
					
				else if(aux.length()==2){
					if(aux.charAt(0)=='1'){
						if(aux.charAt(1)<'0' || aux.charAt(1)>'5')
								flag=1;
					}
					else if(aux.charAt(0)=='0'){
						if(aux.charAt(1)<'1' || aux.charAt(1)>'9')
							flag=1;
					}
					else
						flag=1;
				}
				else
					flag=1;
				
			}
			catch(Exception e){
				Log.w(getClass().getSimpleName(),"ERROR in the field number of searchs",e);
			}
			
			if(!checkboxRapidshare.isChecked() && !checkboxMegaupload.isChecked() && 
					!checkboxMegashares.isChecked() && !checkboxFileserve.isChecked() &&
					!checkboxFilesonic.isChecked() && !checkboxHotfile.isChecked())
				flag=2;
			
			//Error in number
			if(flag==1){
				Toast.makeText(this, "Number between 1-15", Toast.LENGTH_SHORT).show();
			}
			
			//Error in providers
			else if(flag==2){
				Toast.makeText(this, "Almost 1 provider to search", Toast.LENGTH_SHORT).show();
			}
			
			//Save options
			else{
				
				if(checkboxRapidshare.isChecked()){
					Globals.rapidshare=true;
				}
				else{
					Globals.rapidshare=false;
				}
				
				if(checkboxMegaupload.isChecked()){
					Globals.megaupload=true;
				}
				else{
					Globals.megaupload=false;
				}
				
				if(checkboxFileserve.isChecked()){
					Globals.fileserve=true;
				}
				else{
					Globals.fileserve=false;
				}
				
				if(checkboxMegashares.isChecked()){
					Globals.megashares=true;
				}
				else{
					Globals.megashares=false;
				}
				
				if(checkboxHotfile.isChecked()){
					Globals.hotfile=true;
				}
				else{
					Globals.hotfile=false;
				}
				
				if(checkboxFilesonic.isChecked()){
					Globals.filesonic=true;
				}
				else{
					Globals.filesonic=false;
				}
				
				//Saving Spinner
				if(SpinnerPos!=0){
					Globals.extension=StringSpinner;
					Globals.POS_EXTENSION=SpinnerPos;
				}
				else{
					Globals.extension="";
					Globals.POS_EXTENSION=0;
				}
				
				Globals.MAX_SEARCHS=Integer.parseInt(aux);
				
				Toast.makeText(this, "Options Saved", Toast.LENGTH_SHORT).show();
			}
		}	
}
